package com.example.springboot.common.utils.utils;


import com.example.springboot.entity.Permission;

import java.util.ArrayList;
import java.util.List;


public class MenuHelper {
    //使用递归方法建菜单
    public static List<Permission> buildTree(List<Permission> sysMenuList) {
        List<Permission> tree = new ArrayList<>();
        sysMenuList.forEach(item -> {
            if (item.getParentid() == 0) {
                tree.add(getChildren(item, sysMenuList));
            }
        });
        return tree;
    }

    public static Permission getChildren(Permission sysMenu,
                                         List<Permission> sysMenuList) {
        sysMenu.setChildren(new ArrayList<>());
        sysMenuList.forEach(item -> {
            if (item.getParentid().equals(sysMenu.getPid())) {
                sysMenu.getChildren().add(getChildren(item, sysMenuList));
            }
        });
        return sysMenu;
    }
}
